﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Lays.Manage.Core.News.Authorization;
using Lays.Manage.Core.News.Dtos;

namespace Lays.Manage.Core.News
{
    /// <summary>
    /// 新闻服务实现
    /// </summary>
    [AbpAuthorize(NewsAppPermissions.News)]
    public class NewsAppService : AbpZeroTemplateAppServiceBase, INewsAppService
    {
        private readonly IRepository<News, int> _newsRepository;


        private readonly NewsManage _newsManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public NewsAppService(IRepository<News, int> newsRepository, NewsManage newsManage)
        {
            _newsRepository = newsRepository;
            _newsManage = newsManage;

        }

        #region 新闻管理

        /// <summary>
        /// 根据查询条件获取新闻分页列表
        /// </summary>
        public async Task<PagedResultDto<NewsListDto>> GetPagedNewssAsync(GetNewsInput input)
        {

            var query = _newsRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var newsCount = await query.CountAsync();

            var newss = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var newsListDtos = newss.MapTo<List<NewsListDto>>();
            return new PagedResultDto<NewsListDto>(
            newsCount,
            newsListDtos
            );
        }

        /// <summary>
        /// 通过Id获取新闻信息进行编辑或修改 
        /// </summary>
        public async Task<GetNewsForEditOutput> GetNewsForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetNewsForEditOutput();

            NewsEditDto newsEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _newsRepository.GetAsync(input.Id.Value);
                newsEditDto = entity.MapTo<NewsEditDto>();
            }
            else
            {
                newsEditDto = new NewsEditDto();
            }

            output.News = newsEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取新闻ListDto信息
        /// </summary>
        public async Task<NewsListDto> GetNewsByIdAsync(EntityDto<int> input)
        {
            var entity = await _newsRepository.GetAsync(input.Id);

            return entity.MapTo<NewsListDto>();
        }

        /// <summary>
        /// 新增或更改新闻
        /// </summary>
        public async Task CreateOrUpdateNewsAsync(CreateOrUpdateNewsInput input)
        {
            if (input.NewsEditDto.Id.HasValue)
            {
                await UpdateNewsAsync(input.NewsEditDto);
            }
            else
            {
                await CreateNewsAsync(input.NewsEditDto);
            }
        }

        /// <summary>
        /// 新增新闻
        /// </summary>
        [AbpAuthorize(NewsAppPermissions.News_CreateNews)]
        public virtual async Task<NewsEditDto> CreateNewsAsync(NewsEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<News>();

            entity = await _newsRepository.InsertAsync(entity);
            return entity.MapTo<NewsEditDto>();
        }

        /// <summary>
        /// 编辑新闻
        /// </summary>
        [AbpAuthorize(NewsAppPermissions.News_EditNews)]
        public virtual async Task UpdateNewsAsync(NewsEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _newsRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _newsRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        [AbpAuthorize(NewsAppPermissions.News_DeleteNews)]
        public async Task DeleteNewsAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _newsRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除新闻
        /// </summary>
        [AbpAuthorize(NewsAppPermissions.News_DeleteNews)]
        public async Task BatchDeleteNewsAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _newsRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

    }
}
